package com.sh.demo.qa.module.rbac.entity;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Access(AccessType.FIELD)
@Table(name="T_PERMISSION")
public class Permission implements Serializable {

	private static final long serialVersionUID = 8009953431310879289L;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="permission")
	@SequenceGenerator(name="permission", sequenceName="SEQ_PERMISSION")
	@Column(length = 20)
	private Long id;

	@Column(length = 50, nullable = false)
	private String code;
	
	@Column(length = 50)
	private String name;

	@Column(length = 100)
	private String description;

	@ManyToMany(mappedBy = "permissions", fetch = FetchType.LAZY)
	private Collection<Role> roles;

    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, optional = true)
    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    private Permission parent;

    @OneToMany(mappedBy = "parent", cascade = { CascadeType.REMOVE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
    private Collection<Permission> children;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Collection<Role> getRoles() {
		return roles;
	}

	public void setRoles(Collection<Role> roles) {
		this.roles = roles;
	}

	public Permission getParent() {
		return parent;
	}

	public void setParent(Permission parent) {
		this.parent = parent;
	}

	public Collection<Permission> getChildren() {
		return children;
	}

	public void setChildren(Collection<Permission> children) {
		this.children = children;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
